package org.smarthings.things.automation.rest.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.smarthings.things.automation.Action;
import org.smarthings.things.automation.Condition;
import org.smarthings.things.automation.Rule;
import org.smarthings.things.automation.RuleCollection;
import org.smarthings.things.automation.RuleEngine;
import org.smarthings.things.automation.Trigger;

public class MockedRulesRegistry implements RuleEngine {

	private Map<String, Rule> rules = new HashMap<String, Rule>(20);

	@Override
	public Rule registerRule(String name, String[] tags, Trigger trigger, Condition condition,
			Action action) throws IllegalArgumentException {
		Rule rule = RuleBuilder.create().name(name).tags(tags).trigger(trigger)
				.condition(condition).action(action).build();

		rules.put(rule.getUUID(), rule); // TODO store persistently
		return rule;
	}

	@Override
	public void unregisterRule(String ruleUUID) {
		rules.remove(ruleUUID);

	}

	@Override
	public Rule getRule(String ruleUUID) {
		return rules.get(ruleUUID);
	}

	@Override
	public RuleCollection filterRules(String filter) {
		return new MockedRuleCollection(new ArrayList<Rule>(rules.values()));// TODO parse ldap!
	}

	@Override
	public RuleCollection listRules() {
		return new MockedRuleCollection(new ArrayList<Rule>(rules.values()));
	}

	@Override
	public void triggerRule(String ruleUUID) {
		// TODO Auto-generated method stub

	}

}
